package cn.just.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author wujiaming
 */
public class JDBCUtil {
    /**
     * 数据库连接池
     */
    private static DruidDataSource dataSource = null;
    static{
        //读取文件
        Properties properties = new Properties();
        //获取输入流
        InputStream in = cn.just.utils.JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");

        //将配置文件内容保存到Properties
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //读取数据
        String driver = properties.getProperty("db.driver");
        String url = properties.getProperty("db.url");
        String userName = properties.getProperty("db.username");
        String password = properties.getProperty("db.password");
        int initialSize = Integer.parseInt(properties.getProperty("db.initialSize"));
        int maxActive = Integer.parseInt(properties.getProperty("db.maxAxtive"));
        //创建数据库连接池
        dataSource = new DruidDataSource();
        //设置连接池的信息
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        //设置初始化连接数量
        dataSource.setInitialSize(initialSize);
        //设置同一时间可以使用的最大连接数量
        dataSource.setMaxActive(maxActive);
        //setInitialSize<setMaxActive
    }
    public static Connection getConnect() throws SQLException {
        //从连接池中获取连接
        DruidPooledConnection connection = dataSource.getConnection();
        return connection;
    }

    public static void close(Connection conn){
        if(conn !=null){
            try {
                conn.close();//把连接对象放回池子里
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        }
    }

    public static void close(Connection connection, PreparedStatement preparedStatement) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet rs) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        }

        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws SQLException {
        System.out.println(getConnect());
    }
}
